/* eslint-disable indent */
/* eslint-disable no-undef */
import axios from 'axios'
import config from '@/config/config'
// import wx from './WX'
// import store from '@/store'

const apiUrl = config.api
// 判断访问终端
const browser = {
  versions: (function () {
    const u = navigator.userAgent
    return {
      mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
      ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
      app: u.indexOf('zhanye-ios') > -1 // 是否app
    }
  }())
}

export default {
  methods: {
    $upload (file, type = '') { // 上传文件
      return new Promise(resolve => {
        console.log(file)
        let fd = new FormData()
        if (Array.isArray(file)) {
          for (let i in file) {
            fd.append(`img${i}`, file[i].file)
          }
        } else {
          fd.append('img0', file.file)
        }
        axios({
          method: 'POST',
          url: `${apiUrl}/api/uploadfile?type=${type}`,
          data: fd
        }).then(res => {
          if (res.status === 200) {
            resolve(res.data)
          }
        }).catch(_ => {
          reject(false)
        })
      })
    },
    $H5Init () {
      return new Promise((resolve) => {
        if (typeof (AndroidJsBridge) === 'object') {
          this.$store.commit('SET_PL', 'ANDROID')
          if (AndroidJsBridge.getToken()) {
            // eslint-disable-next-line space-in-parens
            const TK = JSON.parse( AndroidJsBridge.getToken() ).token
            const TU = JSON.parse(AndroidJsBridge.getUserInfo())
            localStorage.setItem('token', TK)
            localStorage.setItem('uinfo', JSON.stringify(TU))
            this.$store.commit('SET_TOKEN', TK)
            this.$store.commit('SET_UINFO', TU)
          }
        } else if (browser.versions.mobile && browser.versions.app && browser.versions.ios) {
          // IOS  客户端
          this.$store.commit('SET_PL', 'IOS')
          window.iosRegisterHandler('setToken').then(res => {
            this.$store.commit('SET_TOKEN', res.token)
            localStorage.setItem('token', res.token)
            window.iosRegisterHandler('setUserData').then(res => {
              localStorage.setItem('uinfo', JSON.stringify(res))
              this.$store.commit('SET_UINFO', res)
            })
          })
        } else {
          // H5平台
          resolve(false)
        }
      })
    },
    $img (name) {
      return require(`@/assets/images/${name}`)
    },
    // IOS 手机APP端
    $isIOS () {
      if (browser.versions.mobile && browser.versions.ios && browser.versions.app) {
        return true
      }
      return false
    },
    // rem转px
    $remToPx (rem) {
      return parseFloat(rem) * (document.documentElement.clientWidth / 7.5)
    },
    // 分享
    $shareInit (img, title, desc, url) {
      if (this.$store.state.pl === 'ANDROID') {
        AndroidJsBridge.setShareData(title, img, url, title)
      } else if (this.$store.state.pl === 'IOS') {
        window.iosCallHandler('setShareData', {
          img,
          title,
          desc,
          url
        })
      } else if (this.$store.state.pl === 'H5') {
        if (this.$isWxBrowser()) {
          this.$setWxShareConfig({ img, title, desc, link: url })
        }
      }
    }
  }
}
